<?php

namespace app\index\controller;

use app\common\controller\Bin;
use think\Db;
use tool\SMS;

class Index extends Bin
{
    public function test()
    {
        echo "HEllo world";
    }

    /*
     * 正则
     *  "#<a[^>]+href=(['\"])(.+)\\1#isU"   处理链接
        "#<img[^>]+src=(['\"])(.+)\\1#isU" 处理图片
     * */

    public function index()
    {
        $user = model('user')->where('id', 1)->find();
        session('user', $user['id']);
        $essaylists = model('viewEssay')->select();
        $count = model('discuss')->where('uid', $user['id'])->count('uid');
        foreach ($essaylists as $k => $v) {
            $essaylists[$k]['count'] = $count;
        }
        $this->assign('lists', $essaylists);
        $this->assign('user', $user);
        return view('home');
    }

    protected function getuser()
    {
        return session('user');
    }

    public function about()
    {
        return view();
    }

    public function randDataView()
    {
        return view();
    }

    public function clearTables()
    {
        return view();
    }
    public function submitclear(){
        $table = input('table');
        $status = $this->clearTable($table);
    }
    public function detail()
    {
        $eid = input('eid');
        $list = model('essay')->where('eid', $eid)->find();
        $list['nickname'] = (model('user')->where('id', $list['uid'])->field('nickname')->find()['nickname']);
        $discuss_list = model('discuss')->where('eid', $eid)->order('time', 'desc')->select();
        $this->assign('list', $list);
        $this->assign('discuss_list', $discuss_list);
        return view();
    }

    public function addData()
    {
//        debug('begin');
        $num = input('num');
        $table = input('table');
        if (empty($table)) {
            $this->error("没有指定表名");
        }
        if ($num == 0) {
            $this->error("没有指定生成数据数量");
        }
        $status = $this->randData($table, $num);//生成随机数据
//        debug('end');
        if ($status) {
//            echo debug('begin', 'end') . 's' . "\n\r";
//            echo debug('begin', 'end', 6) . 's' . "\n\r";
//            echo debug('begin', 'end', 'm') . 'kb' . "\n\r";
            echo $status . "\n\r";
            $this->success("添加成功");
        } else {
            $this->success("添加失败");
        }

    }



    public function article()
    {
        $user = $this->getuser();
        $essaylists = model('viewEssay')->select();
        $count = model('discuss')->where('uid', $user)->count('uid');
        foreach ($essaylists as $k => $v) {
            $essaylists[$k]['count'] = $count;
        }
        $label_lists = model('label')->select();
        $this->assign('label_lists', $label_lists);
        $this->assign('lists', $essaylists);
        return view();
    }


    public function resource()
    {
        return view();
    }

    public function timeline()
    {
        return view();
    }

    public function add_detail()
    {
        dump($_POST);
        die();
    }

    /**
     * 短信发送 oos
     * @throws \app\common\controller\HttpResponseException
     */
    public function wapsendsms()
    {
        $uid = 1;
        $tel = input("tel");
        if (!preg_match("/^1[34578]{1}\d{9}$/", $tel)) {
            $this->e("手机号格式不正确");
        }
        $code = random(4);
        cache("reg_code_wap_$uid", ["tel" => $tel, "code" => $code]);
        $sms = new SMS();
        $sms->send("SMS_137215033", $tel, ['code' => $code], "知蜂票务通");

        return $this->success("发送成功");
    }
}
